<?php
/**
 * Created by PhpStorm.
 * User: DELL-PC
 * Date: 2018/8/2
 * Time: 9:50
 */

namespace J\Schema\Collection;


use J\Schema\CollectionManager;
use J\Schema\Collection\Element\ElementManager;
use J\Schema\Syntax;
use J\ReadOnly;

/**
 *
 */
class Collection implements ICollection
{
    /**
     * @var string
     */
    protected $name;

    /**
     * @var CollectionManager 上级管理器
     */
    protected $_collections;

    /**
     * @var ReadOnly 配置
     */
    protected $config;

    /**
     * @var ElementManager 元素管理器
     */
    protected $_elements = null;

    /**
     * @var bool 是否能构造为树结构
     */
    protected $tree = null;

    /**
     * @var CollectionManager[]
     */
    protected $_inners = null;

    /**
     * Collection constructor.
     * @param string $name
     * @param CollectionManager $schema
     * @param array $config
     */
    public function __construct(string $name, CollectionManager $schema, array $config)
    {
        $this->name = strtolower($name);
        $this->_collections = $schema;
        $this->config = new ReadOnly($config);
    }


    public function getCurd()
    {
        return new Curd($this);
    }

//    public function getWhere()
//    {
//        return new Where($this);
//    }
//
//    public function getOption()
//    {
//        return new Option($this);
//    }

    public function getAttribute()
    {
        return $this->config;
    }

    public function getLinker($name)
    {
        return $this->_collections->getLinker($this->name, $name);
    }

    public function getCollection($name)
    {
        return $this->_collections->getCollection($name);
    }

    public function getName()
    {
        return $this->name;
    }

    public function getLabel()
    {
        return $this->getAttribute()->get('label', $this->getName());
    }

    public function getKey()
    {
        return $this->getAttribute()->get(Syntax::KEY_KEY, 'name');
    }

    public function getTreeKey()
    {
        return $this->isTree() ? $this->getName() : false;
    }

    public function isTree()
    {
        if ($this->tree === null) {
            try {
                $this->tree = $this->elements()->hasElement($this->getName());
            } catch (\Throwable $e) {
            }
        }
        return $this->tree;
    }

    public function elements()
    {
        if (!$this->_elements) {
            $this->_elements = new ElementManager($this, $this->_collections->isInner());
        }
        return $this->_elements;
    }

    public function unique($element, $value)
    {
        try {
            $r = $this->getCurd()->setWhere([$element => $value])->setElements(Syntax::KEY_ALL)->count();
            return ($r == 0);
        } catch (\Throwable $e) {
            return false;
        }
    }

    public function getTableName()
    {
        return $this->getAttribute()->get('table', $this->name);
    }

    /**
     * @param string $name
     * @return mixed
     */
    public function belongs($name)
    {
        try {
            return $this->elements()->getElement($name)->isBelongs();
        } catch (\Throwable $e) {
            return false;
        }
    }

    public function inners()
    {
        if (!$this->getAttribute()->get(Syntax::KEY_EXTENDS)) return null;

        if (!$this->_inners === null) {
            $this->_inners = new CollectionManager($this->getAttribute()->get(Syntax::KEY_EXTENDS));
        }

        return $this->_inners;
    }

    /**
     * @return CollectionManager
     */
    public function getCollections(): CollectionManager
    {
        return $this->_collections;
    }
}